#!/usr/bin/python2.4
#
# Copyright 2009-2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ========================================================================

""" Build enterprise installers examples.

  This file contains the product-specific information for building an
  MSI for remote installation in the enterprise. Product teams should only
  need to modify this file.
  APPTEAMS:
  There are two ways to build such an MSI depending on whether the product's
  installer is or contains an MSI.
"""

Import('env')

import urllib
from enterprise.installer import build_enterprise_installer

# APPTEAMS: Change this value to False.
# Build the installer using build rules specific to Google Update builds.
BUILDING_WITH_GOOGLE_UPDATE = True

# We need to build the custom_actions dll.
env.BuildSConscript('custom_actions')

if BUILDING_WITH_GOOGLE_UPDATE and not env.Bit('no-tests'):
  env.BuildSConscript('test')

# APPTEAMS:
# Note about versions: Each released build needs to have a different Build
# number in the version "Major.Minor.Build.Qfe". The Qfe is ignored by MSI and
# only increasing it may cause both the old and new versions of the this MSI
# to exist side-by-side (http://msdn.microsoft.com/en-us/library/aa370859.aspx).

# Custom action DLL name.
# Set this to the path to the DLL containing the error display custom action.
CUSTOM_ACTION_DLL = ('$STAGING_DIR/show_error_action.dll')

#
# Applications with EXE installers
#
# Use this section to build an MSI that contains Google Update and a product's
# EXE installer. The EXE may not use an MSI installer internally.
# MSI installers cannot be wrapped in this way because nested MSI installs are
# discouraged, problematic, and deprecated from WiX.

# APPTEAMS: Make appropriate call to BuildEnterpriseInstaller(). Example below.

# This sample:
#  * Builds multiple "products" (each product is actually a different version).
#    Products are specified in an array of structures, which specify the values
#    for each product.
#  * Builds multiple pre-tagged brands and ap values for each "product."
def _BuildSampleForTest():
  ENTERPRISE_INSTALLER_PRODUCTS = [
    ('Save Arguments', '0.1.2.0', '{7DD1EF7B-D075-47c0-BD51-F624ED87CCF0}',
     '&brand=mkfi',
     '$MAIN_DIR/testing/unittest_support/SaveArguments.exe',
     '/install 0.1.2.0', '/donotregister', '/silentuninstall 0.1.2.0',
     CUSTOM_ACTION_DLL,
     'SaveArguments_Enterprise_Installer_0.1.2.0'),
    ('Save Arguments', '0.1.3.0', '{7DD1EF7B-D075-47c0-BD51-F624ED87CCF0}',
     '&brand=mkfi',
     '$MAIN_DIR/testing/unittest_support/SaveArguments.exe',
     '/install 0.1.3.0', '/donotregister', '/silentuninstall 0.1.3.0',
     CUSTOM_ACTION_DLL,
     'SaveArguments_Enterprise_Installer_0.1.3.0'),
    ]

  # Example of calling BuildEnterpriseInstaller with multiple brands and ap
  # value combinations for each product.
  BRAND_AP_COMBINATIONS = [
    ('FOOB', 'foobar'),
    #('ENTA', 'enterprise'),
    #('ENTB', 'enterprise'),
  ]

  # Omaha only: Build the enterprise installer for each version of Omaha.
  for omaha_version_info in env['omaha_versions_info']:
    prefix = omaha_version_info.filename_prefix

    for combo in BRAND_AP_COMBINATIONS:
      for product in ENTERPRISE_INSTALLER_PRODUCTS:
        (product_name, product_version, product_guid,
         product_custom_params,
         product_installer_path,
         product_installer_install_command,
         product_installer_disable_update_registration_arg,
         product_uninstaller_additional_args,
         show_error_action_dll,
         msi_base_name) = product

        product_custom_params = '&brand=' + combo[0] + '&ap=' + combo[1]

        build_enterprise_installer.BuildEnterpriseInstaller(
            env,
            product_name, product_version, product_guid,
            product_custom_params,
            product_installer_path,
            product_installer_install_command,
            product_installer_disable_update_registration_arg,
            product_uninstaller_additional_args,
            prefix + msi_base_name + '_' + combo[0],
            '$MAIN_DIR/enterprise/installer',
            show_error_action_dll,
            '$STAGING_DIR/%sGoogleUpdateSetup.exe' % prefix,
            output_dir = '$TARGET_ROOT/Test_Installers'
        )

# Builds a sample enterprise installer based on a standalone installer.
# TODO(omaha): Use a standalone installer that does not wrap an MSI. This
# mechanism is not intended to be used for apps that use MSI installers.
# As is, Omaha tries to install the MSI but fails because the Windows
# Installer is busy. It takes a long time to fail because Omaha retries.
# TODO(omaha): This is a bad example because it uses MSI. See above.
def _BuildSampleStandaloneForTest():
  # Omaha only: Build the enterprise installer for each version of Omaha.
  for omaha_version_info in env['omaha_versions_info']:
    prefix = omaha_version_info.filename_prefix

    msi_base_name = 'TestFoo_Installer_1.0.101.0'
    BRAND = 'ENTC'
    AP = 'enterprise-C'
    product_custom_params = '&brand=' + BRAND + '&ap=' + AP

    # Sample installer data, encoded for the command line. This sample uses a
    # JSON format but the format is app-specific.
    product_installer_data = (
        '{"enterprise":{"param1":true,"param2":true},"value1":"foo"}')
    product_installer_data = urllib.quote(product_installer_data)

    build_enterprise_installer.BuildEnterpriseInstallerFromStandaloneInstaller(
        env,
        'Test Foo',
        '1.0.101.0',
        '{D6B08267-B440-4c85-9F79-E195E80D9937}',
        product_custom_params,
        '/silentuninstall',
        product_installer_data,
        ('$TARGET_ROOT/Test_Installers/' +
            '%sUNOFFICIAL_TestFooStandaloneInstaller.exe' % (prefix)),
        CUSTOM_ACTION_DLL,
        prefix + msi_base_name + '_' + BRAND,
        '$MAIN_DIR/enterprise/installer',
        output_dir = '$TARGET_ROOT/Test_Installers'
    )

if BUILDING_WITH_GOOGLE_UPDATE:
  _BuildSampleForTest()
  _BuildSampleStandaloneForTest()


#
# Applications with MSI installers
#
# To bundle Google Update with a product that has an MSI installer:
#  * Build a .wixobj that contains the Google Update installation fragment
#    by calling BuildGoogleUpdateFragment().
#  * Include the ComponentGoogleUpdate component in the appropriate feature in
#    the product's WiX code.
#
# For an example, see instances of "enterprise" in the following files:
#  omaha/test/test_foo.wxs.xml
#  omaha/test/build.scons
